package stewardapp

import (
	"github.com/gin-gonic/gin"
	"github.com/shopspring/decimal"
	"log"
	"strconv"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/logic"
	"yundian/internel/app/models/wifimodel"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
	. "yundian/internel/pkg/dingo/result"
	"yundian/internel/pkg/utils"
)
// HandleStewardAppUserAccountLoginWifi 帐号登录 - WIFI
//func HandleStewardAppUserAccountLoginWifi(ctx *gin.Context) {
//	param := &request.StewardAppUserAccountLoginRes{}
//	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
//	param.Ip = ctx.ClientIP()
//	var loginInfo *request.LoginBack
//	var err error
//
//	if loginInfo, err = logic.NewAuthLogic().UserAccountLoginWIFI(param); err != nil {
//		Response(ctx)(err.Error(), CodeInvalidParam, loginInfo)(OK)
//		return
//	}
//
//	Response(ctx)("登录成功", CodeSuccess, loginInfo)(OK)
//}

// UserLogout 后台管理员退出
func HandleWifiUserLogout(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	if err != nil {
		dingo.ErrorIf(err).Unwrap()
	}
	key := utils.Join("Yd-wifi-Login-Token", strconv.Itoa(userId))
	// 清除redis缓存中的token
	global.Rdb.Del(ctx, key)
	Response(ctx)("成功退出", CodeSuccess, true)(OK)
}

// HandleGetWifiUserInfo 获取用户信息
func HandleGetWifiUserInfo(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	if err != nil {
		dingo.ErrorIf(err).Unwrap()
	}

	// 获取用户信息业务逻辑处理
	userInfo := logic.NewAuthLogic().GetWifiUserInfo(userId)
	parentInfo := logic.NewAuthLogic().GetWifiUserInfo(userInfo.ParentID)
	userInfo.ParentInfo = parentInfo

	log.Println("打印测试用户登录信息", userInfo)
	// 响应请求
	Response(ctx)("", CodeSuccess, userInfo)(OK)
}

func HandleGetWifiAdCashMoney(ctx *gin.Context)  {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()
	// 计算已提现金额(处理成功的)
	cashTotal := dao.NewWifiDao().GetCashLogByUserId(userId).Unwrap().(int64)
	log.Println("打印测试数据--已提现金额", cashTotal)

	// 计算分红总金额
	dividTotal := dao.NewWifiDao().GetDividendsMoneyByUserId(userId).Unwrap().(float64)
	log.Println("打印测试数据--分红总金额", dividTotal)

	// 计算帐期 15天
	dateStr := time.Now().AddDate(0, 0, -7).Format("2006-01-02")
	unCashMoney := dao.NewWifiDao().GetUnCashMoneyByUserId(userId, dateStr).Unwrap().(float64)  // 未入帐金额
	cashMoney := dao.NewWifiDao().GetCashMoneyByUserId(userId, dateStr).Unwrap().(float64) // 已入帐金额
	log.Println("打印测试数据--未入帐，已入帐金额", unCashMoney, cashMoney)

	// 可提现金额 = 已入帐金额 - 已提现金额
	withdrawalMoney, _ := decimal.NewFromFloat(cashMoney).Sub(decimal.NewFromInt(cashTotal).Div(decimal.NewFromInt(100))).Float64()
	log.Println("打印测试数据--可提现金额", withdrawalMoney)


	// 可提现金额 = 分红总金额 - 已提现金额
	Response(ctx)("", CodeSuccess, &struct {
		CashTotal     string `json:"cashTotal"`     // 已提现金额
		DividTotal    string `json:"dividTotal"`    // 分红总额
		UnCashMoney   string `json:"unCashMoney"`     // 未入帐
		CashMoney     string `json:"cashMoney"` // 已入帐
		WithdrawalMoney string `json:"withdrawalMoney"` // 可提现
	}{
		CashTotal:  decimal.NewFromInt(cashTotal).Div(decimal.NewFromFloat(100)).StringFixed(2),
		DividTotal: decimal.NewFromFloat(dividTotal).StringFixed(2),
		UnCashMoney: decimal.NewFromFloat(unCashMoney).StringFixed(2),
		CashMoney: decimal.NewFromFloat(cashMoney).StringFixed(2),
		WithdrawalMoney: decimal.NewFromFloat(withdrawalMoney).StringFixed(2),
	})(OK)
}

// 获取提现记录
func HandleGetWifiAdCashList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()
	param := &request.CashListReq{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()
	param.UserId = userId

	data := dao.NewWifiDao().GetCashList(param)

	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// 获取收益明细
func HandleGetWifiAdIncomeList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.IncomeListReq{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param))

	param.UserId = userId

	data := dao.NewWifiDao().GetIncomeList(param)

	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

func HandleGetWifiAdCashRecord(c *gin.Context) {

	userId, err := helper.GetUserIdFromCtx(c)
	if err != nil {
		Response(c)(err.Error(), CodeNeedLogin, false)(Unauthorized)
		return
	}
	//判断是否存在未处理的订单
	cashLogInfo := dao.NewWifiDao().GetCashLogByInfo(userId).Unwrap().(wifimodel.AdCashLogModel)
	Response(c)("", CodeSuccess, cashLogInfo)(OK)
}